package rakuse7en.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import rakuse7en.packet.MyPacket;

public class UserConnection extends Thread {
	private Socket socket;
	private ObjectInputStream ois;
	private boolean running;
	BattleNetFrame bFrame;
	public UserConnection(BattleNetFrame bFrame, Socket socket, ObjectInputStream o) throws IOException{
		this.socket = socket;
		this.bFrame = bFrame;
		ois = o;
	}
	
	public void run(){
        
        MyPacket mPacket;
        try{
        	running = true;
            while(running){
               
                mPacket = (MyPacket)ois.readObject();
                if(mPacket.getTYPE() == 1){
                	bFrame.logMessage(mPacket.getsData().getIp() +"/"+mPacket.getsData().getName());
                }else{
                	bFrame.logMessage(mPacket.getuData().getUserId()+"/"+mPacket.getuData().getPassword());
                }
            }
        } catch(IOException e){
            System.err.println(e);
            doClose();
            running = false;
            
        } catch (ClassNotFoundException e){
            System.err.println(e);
        }       
	}
	public void doStop(){
		running = false;
		doClose();
	}
	public void doClose(){
		try {ois.close();} catch (IOException e) {}
		try {socket.close();} catch (IOException e) {}
	}
}